package gw.com.android.ui.chart.views;

import android.app.Activity;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.gwtsz.chart.output.utils.ChartConfig;
import com.gwtsz.gts2.hx.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import gw.com.android.app.AppMain;
import gw.com.android.ui.chart.ChartTypeData;
import www.com.library.app.AppActivities;
import www.com.library.dialog.BasePopWindow;
import www.com.library.model.DataItemDetail;
import www.com.library.model.DataItemResult;
import www.com.library.util.CommonUtils;
import www.com.library.util.DeviceUtil;
import www.com.library.util.NetworkMonitor;
import www.com.library.view.RecycleViewDivider;
import www.com.library.view.RecyclerClickListener;

/**
 * Created by reeta.zou on 2016/10/19.
 * 图表的一些菜单弹层
 */
public class ChartMenuPopWindow extends BasePopWindow {
    private int mBgID;
    private DataItemResult mArray;

    public ChartMenuPopWindow(Activity activity, View view, int bgID, DataItemResult mArray, RecyclerClickListener callback) {
        this.mArray = mArray;
        mBgID = bgID;
        initPopWindow(activity, view, R.layout.dialog_chart_menu,callback);
        // 高度自适应
        popWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        popWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        popWindow.setBackgroundDrawable(ContextCompat.getDrawable(activity, R.color.transparent));
        popWindow.setTouchable(true);

        popWindow.setTouchInterceptor(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                return false;
                // 这里如果返回true的话，touch事件将被拦截
                // 拦截后 PopupWindow的onTouchEvent不被调用，这样点击外部区域无法dismiss
            }
        });
    }

    @Override
    public void createPopWindow(View dialogView) {
        dialogView.setBackgroundResource(mBgID);
        popListView = (RecyclerView) dialogView.findViewById(R.id.pop_list);
        popListView.addItemDecoration(new RecycleViewDivider(activity, LinearLayoutManager.VERTICAL,
                R.drawable.chart_menu_divider_bg, false));
        modeListAdapter = new DataAdapter(activity);
        popListView.setAdapter(modeListAdapter);
        // 线性布局管理器
        LinearLayoutManager layoutManager = new LinearLayoutManager(activity);
        // 设置布局管理器
        popListView.setLayoutManager(layoutManager);

    }

    /**
     * 刷新界面数据
     * @param mData
     */
    public void refreshData(DataItemResult mData){
        if(mArray == null){
            mArray = new DataItemResult();
        }else{
            mArray.clear();
        }
        mArray.appendItems(mData);
        modeListAdapter.notifyDataSetChanged();
    }

    @Override
    public void show() {
        if (hasShowing() || modeListAdapter.getItemCount() < 1) {
            return;
        }
        if (!activity.isFinishing()) {
            popWindow.showAsDropDown(view, 0, 0);
        }
    }

    public void show(View rootView) {
        if (hasShowing() || modeListAdapter.getItemCount() < 1) {
            return;
        }
        if (!activity.isFinishing()) {
            popWindow.showAsDropDown(rootView, DeviceUtil.instance().getScreenPixelsWidth(AppActivities.getSingleton().currentActivity()), (int) (-12 * ChartConfig
                .getDensity()));
        }
    }

    /**
     * 列表适配器
     * @author reeta
     *
     */
    public class DataAdapter extends RecyclerView.Adapter{
        private LayoutInflater mInflater;

        public DataAdapter(Activity act) {
            mInflater = LayoutInflater.from(act);
        }

        public DataItemDetail getItem(int position) {
            if (position < 0 || position >= mArray.getDataCount())
                return null;
            return mArray.getItem(position);
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View itemView = mInflater.inflate(R.layout.list_item_chart_menu_pop, parent,false);
            return new ItemView(itemView);
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            ItemView viewHold = (ItemView) holder;
            if (viewHold.mView != null) {
                DataItemDetail mItem = getItem(position);
                viewHold.mView.setText(AppMain.getAppString(mItem.getInt("title")));
                viewHold.mView.setCompoundDrawablesWithIntrinsicBounds(mItem.getInt("imageID"),0, 0, 0);
                viewHold.mView.setTag(position);
            }
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public int getItemCount() {
            if(null != mArray){
                return mArray.getDataCount();
            }
            return 0;
        }

        class ItemView extends RecyclerView.ViewHolder{

            @BindView(R.id.item_title)
            TextView mView;

            public ItemView(View itemView) {
                super(itemView);
                ButterKnife.bind(this, itemView);
            }

            @OnClick(R.id.item_layout)
            void onClick() {
                if(CommonUtils.isFastDoubleClick()){
                    return;
                }
                if(null != callback){
                    int position = (int) mView.getTag();
                    hidden();
//                    popWindow.setFocusable(false);
                    popWindow.update();
                    String selectStr = ChartTypeData.instance().getPopPeriodName(position);
                    if(!NetworkMonitor.hasNetWork()){
                        return;
                    }
                    callback.onClick(position, mArray.getItem(position));
                }
            }
        }
    }
}
